package com.budwk.app.sqfwV2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.collection.CollectionUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.Sqfw2ExcelGPForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.Sqfw2PdfGPForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelGenerator;
import com.budwk.app.sqfwV2.service.centuryCity.CenturyCityDownFileTypesResVO;
import com.budwk.app.sqfwV2.service.centuryCity.CenturyCityOldCareExcelService;
import com.budwk.app.sqfwV2.service.centuryCity.CenturyCityOldCarePdfService;
import com.budwk.app.sqfwV2.service.centuryCity.CenturyCityOldCareService;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ZY.Wang
 * @since 2024/6/6
 */
@IocBean
@At("/platform/sqfw/centuryCityOldCare")
@ApiDefinition(tag = "世纪城养老服务站")
@Slf4j
public class CenturyCityOldCareController {

    @Inject
    private CenturyCityOldCareService centuryCityOldCareService;
    @Inject
    private CenturyCityOldCarePdfService centuryCityOldCarePdfService;
    @Inject
    private CenturyCityOldCareExcelService centuryCityOldCareExcelService;
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    /**
     * 养老服务站的app查询列表，获取详情，删除详情目前均使用{@link SummarySafetyController}中的接口
     * 后期如果需要定制才进行单独编写
     **/

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "世纪城养老服务站添加检查数据")
    @ApiResponses
    @SLog(tag = "世纪城养老服务站添加检查数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> addCheckData(@Param("..") Sqfw2PdfGPForController paraData) {
        String userId = SecurityUtil.getUserId();
        paraData.getFormSummary().setCreateBy(userId);
        Sqfw_formSummary_new formSummaryNew = centuryCityOldCarePdfService.createPdf(paraData);
        return Result.success(formSummaryNew);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "重新生成pdf")
    @ApiResponses
    @SLog(tag = "重新生成pdf", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> reCreatPdf(@Param("formSummaryId") String formSummaryId) {
        String userId = SecurityUtil.getUserId();
        Sqfw_formSummary_new formSummaryNew = sqfwFormsummaryNewService.fetch(formSummaryId);
        Sqfw2PdfGPForController paramForPdfGP = new Sqfw2PdfGPForController();
        paramForPdfGP.setUserId(userId);
        paramForPdfGP.setFormSummary(formSummaryNew);
        centuryCityOldCarePdfService.reCreatePdfAndMsg(paramForPdfGP);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "世纪城养老服务站生成材料详情")
    @ApiResponses
    @SLog(tag = "世纪城养老服务站生成材料详情", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getDownloadFileDetail(@Param("fillStartDate") Date startDate, @Param("fillEndDate") Date endDate) {
        String userId = SecurityUtil.getUserId();
        Map<String, Object> resultMap = centuryCityOldCareService.getDownloadFileDetail(userId, startDate, endDate);
        return Result.success(resultMap);
    }


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "世纪城养老服务站excel下载")
    @ApiResponses
    @SLog(tag = "世纪城养老服务站excel下载", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> downloadExcel(@Param("..") Sqfw2ExcelGPForController paramForController) {
        List<ResultOfExcelGenerator> resultList = centuryCityOldCareExcelService.createExcel(paramForController);
        String excelUrl = resultList.get(0).getExcelUrl();
        return Result.data(excelUrl);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "世纪城养老服务站pdf下载")
    @ApiResponses
    @SLog(tag = "世纪城养老服务站pdf下载", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> downloadPdf(@Param("fillStartDate") Date fillStartDate
            , @Param("fillEndDate") Date fillEndDate
            , @Param("fileIdentity") String fileIdentity) {
        String pdfUrl = centuryCityOldCareExcelService.downloadPdf(fillStartDate, fillEndDate, fileIdentity);
        return Result.data(pdfUrl);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "查询养老站")
    @ApiResponses
    @SLog(tag = "查询养老站", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> listOldCarePlaceName(@Param("placeName") String placeName) {
        List<Sqfw_formSummary_new> dataList = centuryCityOldCareExcelService.listOldCarePlaceName(placeName);
        return Result.data(dataList);
    }
}
